package com.sjzd.weixin.utils;

import com.sjzd.weixin.bean.message.MessageType;
import com.sjzd.weixin.bean.message.plain.resp.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

/**
 *  微信消息处理工具类
 */
public class MessageUtils {
    /**
     *  解析微信服务器发来的xml字节流
     * @param in
     * @return
     */
    public static HashMap<String, String> parseXML(InputStream in) throws DocumentException {
        HashMap<String, String> map = new HashMap<>();
        SAXReader reader = new SAXReader();
        Document document = reader.read(in);
        Element root = document.getRootElement();
        List<Element> elements = root.elements();

        for (Element e : elements) {
            map.put(e.getName(), e.getTextTrim());
        }
        return map;
    }

    /**
     *  将回复信息包装成xml格式的字符串形式
     * @param respMsg
     * @return
     */
    public static String createXML(BaseMessage respMsg) {
        Document document= DocumentHelper.createDocument();
        Element rootEle = document.addElement("xml");
        rootEle.addElement("ToUserName").addCDATA(respMsg.getToUserName());
        rootEle.addElement("FromUserName").addCDATA(respMsg.getFromUserName());
        rootEle.addElement("CreateTime").addCDATA(respMsg.getCreateTime());
        rootEle.addElement("MsgType").addCDATA(respMsg.getMsgType());

        switch (respMsg.getMsgType()) {
            case MessageType.RESP_MESSAGE_TYPE_TEXT :
                TextMessage text = (TextMessage) respMsg;
                rootEle.addElement("Content").addCDATA(text.getContent());
                break;
            case MessageType.RESP_MESSAGE_TYPE_MUSIC :
                MusicMessage music = (MusicMessage) respMsg;
                Element musicEle= rootEle.addElement("Music");
                musicEle.addElement("Title").addCDATA(music.getTitle());
                musicEle.addElement("Description").addCDATA(music.getDescription());
                musicEle.addElement("MusicUrl").addCDATA(music.getMusicUrl());
                musicEle.addElement("HQMusicUrl").addCDATA(music.getHqMusicUrl());
                musicEle.addElement("ThumbMediaId").addCDATA(music.getThumbMediaId());
                break;
            case MessageType.RESP_MESSAGE_TYPE_NEWS :
                NewsMessage news = (NewsMessage) respMsg;
                rootEle.addElement("ArticleCount").addCDATA(news.getArticleCount());
                Element articlesEle = rootEle.addElement("Articles");
                for (Article article : news.getArticles()) {
                    Element itemEle = articlesEle.addElement("item");
                    itemEle.addElement("Title").addCDATA(article.getTitle());
                    itemEle.addElement("Description").addCDATA(article.getDescription());
                    itemEle.addElement("PicUrl").addCDATA(article.getPicUrl());
                    itemEle.addElement("Url").addCDATA(article.getUrl());
                }
                break;
        }

        String result =document.getRootElement().asXML();  //去掉头信息文件
        return result;
    }



}
